
<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml" lang="zh_CN">
  <head>
    <meta charset="utf-8" />
    <title>__future__ --- Future 语句定义 &#8212; Python 3.7.8 文档</title>
    <link rel="stylesheet" href="../_static/pydoctheme.css" type="text/css" />
    <link rel="stylesheet" href="../_static/pygments.css" type="text/css" />
    
    <script type="text/javascript" id="documentation_options" data-url_root="../" src="../_static/documentation_options.js"></script>
    <script type="text/javascript" src="../_static/jquery.js"></script>
    <script type="text/javascript" src="../_static/underscore.js"></script>
    <script type="text/javascript" src="../_static/doctools.js"></script>
    <script type="text/javascript" src="../_static/language_data.js"></script>
    <script type="text/javascript" src="../_static/translations.js"></script>
    
    <script type="text/javascript" src="../_static/sidebar.js"></script>
    
    <link rel="search" type="application/opensearchdescription+xml"
          title="在 Python 3.7.8 文档 中搜索"
          href="../_static/opensearch.xml"/>
    <link rel="author" title="关于这些文档" href="../about.html" />
    <link rel="index" title="索引" href="../genindex.html" />
    <link rel="search" title="搜索" href="../search.html" />
    <link rel="copyright" title="版权所有" href="../copyright.html" />
    <link rel="next" title="gc --- 垃圾回收器接口" href="gc.html" />
    <link rel="prev" title="traceback --- 打印或检索堆栈回溯" href="traceback.html" />
    <link rel="shortcut icon" type="image/png" href="../_static/py.png" />
    <link rel="canonical" href="https://docs.python.org/3/library/__future__.html" />
    
    <script type="text/javascript" src="../_static/copybutton.js"></script>
    
    
    
    
    <style>
      @media only screen {
        table.full-width-table {
            width: 100%;
        }
      }
    </style>
 

  </head><body>
  
    <div class="related" role="navigation" aria-label="related navigation">
      <h3>导航</h3>
      <ul>
        <li class="right" style="margin-right: 10px">
          <a href="../genindex.html" title="总目录"
             accesskey="I">索引</a></li>
        <li class="right" >
          <a href="../py-modindex.html" title="Python 模块索引"
             >模块</a> |</li>
        <li class="right" >
          <a href="gc.html" title="gc --- 垃圾回收器接口"
             accesskey="N">下一页</a> |</li>
        <li class="right" >
          <a href="traceback.html" title="traceback --- 打印或检索堆栈回溯"
             accesskey="P">上一页</a> |</li>
        <li><img src="../_static/py.png" alt=""
                 style="vertical-align: middle; margin-top: -1px"/></li>
        <li><a href="https://www.python.org/">Python</a> &#187;</li>
        <li>
          <a href="../index.html">3.7.8 Documentation</a> &#187;
        </li>

          <li class="nav-item nav-item-1"><a href="index.html" >Python 标准库</a> &#187;</li>
          <li class="nav-item nav-item-2"><a href="python.html" accesskey="U">Python运行时服务</a> &#187;</li>
    <li class="right">
        

    <div class="inline-search" style="display: none" role="search">
        <form class="inline-search" action="../search.html" method="get">
          <input placeholder="快速搜索" type="text" name="q" />
          <input type="submit" value="转向" />
          <input type="hidden" name="check_keywords" value="yes" />
          <input type="hidden" name="area" value="default" />
        </form>
    </div>
    <script type="text/javascript">$('.inline-search').show(0);</script>
         |
    </li>

      </ul>
    </div>    

    <div class="document">
      <div class="documentwrapper">
        <div class="bodywrapper">
          <div class="body" role="main">
            
  <div class="section" id="module-__future__">
<span id="future-future-statement-definitions"></span><h1><a class="reference internal" href="#module-__future__" title="__future__: Future statement definitions"><code class="xref py py-mod docutils literal notranslate"><span class="pre">__future__</span></code></a> --- Future 语句定义<a class="headerlink" href="#module-__future__" title="永久链接至标题">¶</a></h1>
<p><strong>源代码：</strong> <a class="reference external" href="https://github.com/python/cpython/tree/3.7/Lib/__future__.py">Lib/__future__.py</a></p>
<hr class="docutils" />
<p><a class="reference internal" href="#module-__future__" title="__future__: Future statement definitions"><code class="xref py py-mod docutils literal notranslate"><span class="pre">__future__</span></code></a> 是一个真正的模块，这主要有 3 个原因：</p>
<ul class="simple">
<li><p>避免混淆已有的分析 import 语句并查找 import 的模块的工具。</p></li>
<li><p>确保 <a class="reference internal" href="../reference/simple_stmts.html#future"><span class="std std-ref">future 语句</span></a> 在 2.1 之前的版本运行时至少能抛出 runtime 异常（import <a class="reference internal" href="#module-__future__" title="__future__: Future statement definitions"><code class="xref py py-mod docutils literal notranslate"><span class="pre">__future__</span></code></a> 会失败，因为 2.1 版本之前没有这个模块）。</p></li>
<li><p>当引入不兼容的修改时，可以记录其引入的时间以及强制使用的时间。这是一种可执行的文档，并且可以通过 import <a class="reference internal" href="#module-__future__" title="__future__: Future statement definitions"><code class="xref py py-mod docutils literal notranslate"><span class="pre">__future__</span></code></a> 来做程序性的检查。</p></li>
</ul>
<p><code class="file docutils literal notranslate"><span class="pre">__future__.py</span></code> 中的每一条语句都是以下格式的：</p>
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="n">FeatureName</span> <span class="o">=</span> <span class="n">_Feature</span><span class="p">(</span><span class="n">OptionalRelease</span><span class="p">,</span> <span class="n">MandatoryRelease</span><span class="p">,</span>
                       <span class="n">CompilerFlag</span><span class="p">)</span>
</pre></div>
</div>
<p>通常 <em>OptionalRelease</em> 要比 <em>MandatoryRelease</em> 小，并且都是和 <a class="reference internal" href="sys.html#sys.version_info" title="sys.version_info"><code class="xref py py-data docutils literal notranslate"><span class="pre">sys.version_info</span></code></a> 格式一致的 5 元素元组。</p>
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="p">(</span><span class="n">PY_MAJOR_VERSION</span><span class="p">,</span> <span class="c1"># the 2 in 2.1.0a3; an int</span>
 <span class="n">PY_MINOR_VERSION</span><span class="p">,</span> <span class="c1"># the 1; an int</span>
 <span class="n">PY_MICRO_VERSION</span><span class="p">,</span> <span class="c1"># the 0; an int</span>
 <span class="n">PY_RELEASE_LEVEL</span><span class="p">,</span> <span class="c1"># &quot;alpha&quot;, &quot;beta&quot;, &quot;candidate&quot; or &quot;final&quot;; string</span>
 <span class="n">PY_RELEASE_SERIAL</span> <span class="c1"># the 3; an int</span>
<span class="p">)</span>
</pre></div>
</div>
<p><em>OptionalRelease</em> 记录了一个特性首次发布时的 Python 版本。</p>
<p>在 <em>MandatoryRelases</em> 还没有发布时，<em>MandatoryRelease</em> 表示该特性会变成语言的一部分的预测时间。</p>
<p>其他情况下，<em>MandatoryRelease</em> 用来记录这个特性是何时成为语言的一部分的。从该版本往后，使用该特性将不需要 future 语句，不过很多人还是会加上对应的 import。</p>
<p><em>MandatoryRelease</em> 也可能是 <code class="docutils literal notranslate"><span class="pre">None</span></code>, 表示这个特性已经被撤销。</p>
<p><code class="xref py py-class docutils literal notranslate"><span class="pre">_Feature</span></code> 类的实例有两个对应的方法，<code class="xref py py-meth docutils literal notranslate"><span class="pre">getOptionalRelease()</span></code> 和 <code class="xref py py-meth docutils literal notranslate"><span class="pre">getMandatoryRelease()</span></code>。</p>
<p><em>CompilerFlag</em> 是一个（位）标记，对于动态编译的代码，需要将这个标记作为第四个参数传入内建函数 <a class="reference internal" href="functions.html#compile" title="compile"><code class="xref py py-func docutils literal notranslate"><span class="pre">compile()</span></code></a> 中以开启对应的特性。这个标记存储在 <code class="xref py py-class docutils literal notranslate"><span class="pre">_Feature</span></code> 类实例的 <code class="xref py py-attr docutils literal notranslate"><span class="pre">compiler_flag</span></code> 属性中。</p>
<p><a class="reference internal" href="#module-__future__" title="__future__: Future statement definitions"><code class="xref py py-mod docutils literal notranslate"><span class="pre">__future__</span></code></a> 中不会删除特性的描述。从 Python 2.1 中首次加入以来，通过这种方式引入了以下特性：</p>
<table class="docutils align-default">
<colgroup>
<col style="width: 20%" />
<col style="width: 14%" />
<col style="width: 16%" />
<col style="width: 50%" />
</colgroup>
<thead>
<tr class="row-odd"><th class="head"><p>特性</p></th>
<th class="head"><p>可选版本</p></th>
<th class="head"><p>强制加入版本</p></th>
<th class="head"><p>效果</p></th>
</tr>
</thead>
<tbody>
<tr class="row-even"><td><p>nested_scopes</p></td>
<td><p>2.1.0b1</p></td>
<td><p>2.2</p></td>
<td><p><span class="target" id="index-17"></span><a class="pep reference external" href="https://www.python.org/dev/peps/pep-0227"><strong>PEP 227</strong></a>: <em>静态嵌套作用域</em></p></td>
</tr>
<tr class="row-odd"><td><p>generators</p></td>
<td><p>2.2.0a1</p></td>
<td><p>2.3</p></td>
<td><p><span class="target" id="index-18"></span><a class="pep reference external" href="https://www.python.org/dev/peps/pep-0255"><strong>PEP 255</strong></a>: <em>简单生成器</em></p></td>
</tr>
<tr class="row-even"><td><p>division</p></td>
<td><p>2.2.0a2</p></td>
<td><p>3.0</p></td>
<td><p><span class="target" id="index-19"></span><a class="pep reference external" href="https://www.python.org/dev/peps/pep-0238"><strong>PEP 238</strong></a>: <em>修改除法运算符</em></p></td>
</tr>
<tr class="row-odd"><td><p>absolute_import</p></td>
<td><p>2.5.0a1</p></td>
<td><p>3.0</p></td>
<td><p><span class="target" id="index-20"></span><a class="pep reference external" href="https://www.python.org/dev/peps/pep-0328"><strong>PEP 328</strong></a>: <em>导入：多行与绝对/相对</em></p></td>
</tr>
<tr class="row-even"><td><p>with_statement</p></td>
<td><p>2.5.0a1</p></td>
<td><p>2.6</p></td>
<td><p><span class="target" id="index-21"></span><a class="pep reference external" href="https://www.python.org/dev/peps/pep-0343"><strong>PEP 343</strong></a>: * &quot;with&quot; 语句*</p></td>
</tr>
<tr class="row-odd"><td><p>print_function</p></td>
<td><p>2.6.0a2</p></td>
<td><p>3.0</p></td>
<td><p><span class="target" id="index-22"></span><a class="pep reference external" href="https://www.python.org/dev/peps/pep-3105"><strong>PEP 3105</strong></a>: <em>print 改为函数</em></p></td>
</tr>
<tr class="row-even"><td><p>unicode_literals</p></td>
<td><p>2.6.0a2</p></td>
<td><p>3.0</p></td>
<td><p><span class="target" id="index-23"></span><a class="pep reference external" href="https://www.python.org/dev/peps/pep-3112"><strong>PEP 3112</strong></a>: <em>Python 3000 中的字节字面值</em></p></td>
</tr>
<tr class="row-odd"><td><p>generator_stop</p></td>
<td><p>3.5.0b1</p></td>
<td><p>3.7</p></td>
<td><p><span class="target" id="index-24"></span><a class="pep reference external" href="https://www.python.org/dev/peps/pep-0479"><strong>PEP 479</strong></a>: <em>在生成器中处理 StopIteration</em></p></td>
</tr>
<tr class="row-even"><td><p>annotations</p></td>
<td><p>3.7.0b1</p></td>
<td><p>4.0</p></td>
<td><p><span class="target" id="index-8"></span><a class="pep reference external" href="https://www.python.org/dev/peps/pep-0563"><strong>PEP 563</strong></a>:
<em>Postponed evaluation of annotations</em></p></td>
</tr>
</tbody>
</table>
<div class="admonition seealso">
<p class="admonition-title">参见</p>
<dl class="simple">
<dt><a class="reference internal" href="../reference/simple_stmts.html#future"><span class="std std-ref">future 语句</span></a></dt><dd><p>编译器怎样处理 future import。</p>
</dd>
</dl>
</div>
</div>


          </div>
        </div>
      </div>
      <div class="sphinxsidebar" role="navigation" aria-label="main navigation">
        <div class="sphinxsidebarwrapper">
  <h4>上一个主题</h4>
  <p class="topless"><a href="traceback.html"
                        title="上一章"><code class="xref py py-mod docutils literal notranslate"><span class="pre">traceback</span></code> --- 打印或检索堆栈回溯</a></p>
  <h4>下一个主题</h4>
  <p class="topless"><a href="gc.html"
                        title="下一章"><code class="xref py py-mod docutils literal notranslate"><span class="pre">gc</span></code> --- 垃圾回收器接口</a></p>
  <div role="note" aria-label="source link">
    <h3>本页</h3>
    <ul class="this-page-menu">
      <li><a href="../bugs.html">提交 Bug</a></li>
      <li>
        <a href="https://github.com/python/cpython/blob/3.7/Doc/library/__future__.rst"
            rel="nofollow">显示源代码
        </a>
      </li>
    </ul>
  </div>
        </div>
      </div>
      <div class="clearer"></div>
    </div>  
    <div class="related" role="navigation" aria-label="related navigation">
      <h3>导航</h3>
      <ul>
        <li class="right" style="margin-right: 10px">
          <a href="../genindex.html" title="总目录"
             >索引</a></li>
        <li class="right" >
          <a href="../py-modindex.html" title="Python 模块索引"
             >模块</a> |</li>
        <li class="right" >
          <a href="gc.html" title="gc --- 垃圾回收器接口"
             >下一页</a> |</li>
        <li class="right" >
          <a href="traceback.html" title="traceback --- 打印或检索堆栈回溯"
             >上一页</a> |</li>
        <li><img src="../_static/py.png" alt=""
                 style="vertical-align: middle; margin-top: -1px"/></li>
        <li><a href="https://www.python.org/">Python</a> &#187;</li>
        <li>
          <a href="../index.html">3.7.8 Documentation</a> &#187;
        </li>

          <li class="nav-item nav-item-1"><a href="index.html" >Python 标准库</a> &#187;</li>
          <li class="nav-item nav-item-2"><a href="python.html" >Python运行时服务</a> &#187;</li>
    <li class="right">
        

    <div class="inline-search" style="display: none" role="search">
        <form class="inline-search" action="../search.html" method="get">
          <input placeholder="快速搜索" type="text" name="q" />
          <input type="submit" value="转向" />
          <input type="hidden" name="check_keywords" value="yes" />
          <input type="hidden" name="area" value="default" />
        </form>
    </div>
    <script type="text/javascript">$('.inline-search').show(0);</script>
         |
    </li>

      </ul>
    </div>  
    <div class="footer">
    &copy; <a href="../copyright.html">版权所有</a> 2001-2020, Python Software Foundation.
    <br />
    Python 软件基金会是一个非盈利组织。
    <a href="https://www.python.org/psf/donations/">请捐助。</a>
    <br />
    最后更新于 6月 29, 2020.
    <a href="../bugs.html">发现了问题</a>？
    <br />
    使用<a href="http://sphinx.pocoo.org/">Sphinx</a>2.3.1 创建。
    </div>

  </body>
</html>